package com.xiaomo.gitee.utils;

import com.xiaomo.gitee.ChatServerSide;
import com.xiaomo.gitee.data.Userinfo;
import com.xiaomo.gitee.mapper.UserinfoMapper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.File;
import java.util.List;

public class SQLUtils{

    private static UserinfoMapper userinfoMapper;

    public static void initMybatis() {
        try {
            //XMLUtils.setMybatisConfigUrl("jdbc:mysql://localhost:3306/hazers_chat?serverTimezone=Asia/Shanghai",new File(String.valueOf(ChatServerSide.class.newInstance().getClass().getClassLoader().getResource("userInfoMybatis.xml")).substring(6).replace("My%20Git","My Git")));
            SqlSessionFactory builder = new SqlSessionFactoryBuilder().build(ChatServerSide.class.getClassLoader().getResourceAsStream("userInfoMybatis.xml"));
            userinfoMapper = builder.getConfiguration().getMapper(UserinfoMapper.class, builder.openSession(true));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean hasNameByName(String name) {
        return userinfoMapper.findNameByName(name).size() != 0;
    }

    public static int insert(Userinfo record) {
        return userinfoMapper.insert(record);
    }

    public static List<String> getPasswordByName(String name) {
        return userinfoMapper.getPasswordByName(name);
    }
}
